import { defineStore } from "pinia";
import { defaultJsonSchemaMap } from "@/configs/defaultStatus/defaultJsonSchemaMap";
import {
  addOptionStatusItem,
  deleteOptionStatusItem,
  setTextStatus,
  setStatusIndex,
  setPicSelectOptionItem,
  changeEditorVisible,
  setRateIsUse,
} from "./commonActions";
import type { IComsJsonSchemaMap, IBaseComsUnions } from "@/types/store";
import { getPreSettingComsInitialStatus } from "@/utils/preSetting";

export const useMaterialStore = defineStore("material", () => {
  // - 记录 所有业务组件 对应的默认状态
  const coms = ref<IComsJsonSchemaMap>({
    "single-select": defaultJsonSchemaMap["single-select"](),
    "single-pic-select": defaultJsonSchemaMap["single-pic-select"](),
    "text-note": defaultJsonSchemaMap["text-note"](),
    "multiple-select": defaultJsonSchemaMap["multiple-select"](),
    "option-select": defaultJsonSchemaMap["option-select"](),
    "multiple-pic-select": defaultJsonSchemaMap["multiple-pic-select"](),
    "text-input": defaultJsonSchemaMap["text-input"](),
    "rate-score": defaultJsonSchemaMap["rate-score"](),
    "date-select": defaultJsonSchemaMap["date-select"](),
    // * 预设组件的默认状态
    ...getPreSettingComsInitialStatus(),
  });

  // - 记录当前选中的业务组件
  const currentComJsonSchemaName = ref<IBaseComsUnions>("single-select");

  // * 设置当前选中的业务组件的Json Schema
  const setCurrentComJsonSchemaName = (comName: IBaseComsUnions) => {
    // console.log(comName, coms.value[comName]);
    currentComJsonSchemaName.value = comName;
  };

  return {
    coms,
    currentComJsonSchemaName,
    setTextStatus,
    addOptionStatusItem,
    deleteOptionStatusItem,
    setStatusIndex,
    setCurrentComJsonSchemaName,
    setPicSelectOptionItem,
    changeEditorVisible,
    setRateIsUse,
  };
});
